package com.lvan.schedule.controller;

import com.lvan.schedule.task.CronTaskRegistrar;
import com.lvan.schedule.task.SchedulingRunnable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TaskController {

    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;

    /**
     * @param cron "0/10 * * * * ?"
     */
    @PostMapping("create/task")
    public String createNotParamTask(String cron) {
        SchedulingRunnable runnable = new SchedulingRunnable("workTask", "taskNoParams");
        boolean bool = cronTaskRegistrar.addCronTask(runnable, cron);
        return bool ? "success" : "fail";
    }

    @PostMapping("stop/task")
    public String stopNotParamTask() {
        SchedulingRunnable runnable = new SchedulingRunnable("workTask", "taskNoParams");
        boolean bool = cronTaskRegistrar.removeCronTask(runnable);
        return bool ? "success" : "fail";
    }

    @PostMapping("create/task/params")
    public String createParamTask(String cron, String name, String level) {
        SchedulingRunnable runnable = new SchedulingRunnable("workTask", "taskWithParams", name, level);
        boolean bool = cronTaskRegistrar.addCronTask(runnable, cron);
        return bool ? "success" : "fail";
    }

    @PostMapping("stop/task/params")
    public String stopParamTask(String name, String level) {
        SchedulingRunnable runnable = new SchedulingRunnable("workTask", "taskWithParams", name, level);
        boolean bool = cronTaskRegistrar.removeCronTask(runnable);
        return bool ? "success" : "fail";
    }
}
